home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / filesys.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  16KB  |  625 lines

  1. /* filesys.c -- File system specific functions for hacking this system. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/file.h>
  28. #include <sys/errno.h>
  29. #include "general.h"
  30. #include "tilde.h"
  31. #include "filesys.h"
  32.  
  33. #if !defined (O_RDONLY)
  34. #if defined (HAVE_SYS_FCNTL_H)
  35. #include <sys/fcntl.h>
  36. #else /* !HAVE_SYS_FCNTL_H */
  37. #include <fcntl.h>
  38. #endif /* !HAVE_SYS_FCNTL_H */
  39. #endif /* !O_RDONLY */
  40.  
  41. #if !defined (errno)
  42. extern int errno;
  43. #endif /* !errno */
  44.  
  45. /* Found in info-utils.c. */
  46. extern char *filename_non_directory ();
  47.  
  48. #if !defined (BUILDING_LIBRARY)
  49. /* Found in session.c */
  50. extern int info_windows_initialized_p;
  51.  
  52. /* Found in window.c. */
  53. extern void message_in_echo_area (), unmessage_in_echo_area ();
  54. #endif /* !BUILDING_LIBRARY */
  55.  
  56. /* Local to this file. */
  57. static char *info_file_in_path (), *lookup_info_filename ();
  58. static void remember_info_filename (), maybe_initialize_infopath ();
  59.  
  60. #if !defined (NULL)
  61. #  define NULL 0x0
  62. #endif /* !NULL */
  63.  
  64. typedef struct {
  65.   char *suffix;
  66.   char *decompressor;
  67. } COMPRESSION_ALIST;
  68.  
  69. static char *info_suffixes[] = {
  70.   "",
  71.   ".info",
  72.   "-info",
  73.   (char *)NULL
  74. };
  75.  
  76. static COMPRESSION_ALIST compress_suffixes[] = {
  77.   { ".Z", "uncompress" },
  78.   { ".Y", "unyabba" },
  79.   { ".z", "gunzip" },
  80.   { (char *)NULL, (char *)NULL }
  81. };
  82.  
  83. /* The path on which we look for info files.  You can initialize this
  84.    from the environment variable INFOPATH if there is one, or you can
  85.    call info_add_path () to add paths to the beginning or end of it.
  86.    You can call zap_infopath () to make the path go away. */
  87. char *infopath = (char *)NULL;
  88. static int infopath_size = 0;
  89.  
  90. /* Expand the filename in PARTIAL to make a real name for this operating
  91.    system.  This looks in INFO_PATHS in order to find the correct file.
  92.    If it can't find the file, it returns NULL. */
  93. static char *local_temp_filename = (char *)NULL;
  94. static int local_temp_filename_size = 0;
  95.  
  96. char *
  97. info_find_fullpath (partial)
  98.      char *partial;
  99. {
  100.   int initial_character;
  101.   char *temp;
  102.  
  103.   filesys_error_number = 0;
  104.  
  105.   maybe_initialize_infopath ();
  106.  
  107.   if (partial && (initial_character = *partial))
  108.     {
  109.       char *expansion;
  110.  
  111.       expansion = lookup_info_filename (partial);
  112.  
  113.       if (expansion)
  114.     return (expansion);
  115.  
  116.       /* If we have the full path to this file, we still may have to add
  117.      various extensions to it.  I guess we have to stat this file
  118.      after all. */
  119.       if (initial_character == '/')
  120.     temp = info_file_in_path (partial + 1, "/");
  121.       else if (initial_character == '~')
  122.     {
  123.       expansion = tilde_expand_word (partial);
  124.       if (*expansion == '/')
  125.         {
  126.           temp = info_file_in_path (expansion + 1, "/");
  127.           free (expansion);
  128.         }
  129.       else
  130.         temp = expansion;
  131.     }
  132.       else if (initial_character == '.' &&
  133.            (partial[1] == '/' || (partial[1] == '.' && partial[2] == '/')))
  134.     {
  135.       if (local_temp_filename_size < 1024)
  136.         local_temp_filename = (char *)xrealloc
  137.           (local_temp_filename, (local_temp_filename_size = 1024));
  138. #if defined (HAVE_GETCWD)
  139.       if (!getcwd (local_temp_filename, local_temp_filename_size))
  140. #else /*  !HAVE_GETCWD */
  141.       if (!getwd (local_temp_filename))
  142. #endif /* !HAVE_GETCWD */
  143.         {
  144.           filesys_error_number = errno;
  145.           return (partial);
  146.         }
  147.  
  148.       strcat (local_temp_filename, "/");
  149.       strcat (local_temp_filename, partial);
  150.       return (local_temp_filename);
  151.     }
  152.       else
  153.     temp = info_file_in_path (partial, infopath);
  154.  
  155.       if (temp)
  156.     {
  157.       remember_info_filename (partial, temp);
  158.       if (strlen (temp) > local_temp_filename_size)
  159.         local_temp_filename = (char *) xrealloc
  160.           (local_temp_filename,
  161.            (local_temp_filename_size = (50 + strlen (temp))));
  162.       strcpy (local_temp_filename, temp);
  163.       free (temp);
  164.       return (local_temp_filename);
  165.     }
  166.     }
  167.   return (partial);
  168. }
  169.  
  170. /* Scan the list of directories in PATH looking for FILENAME.  If we find
  171.    one that is a regular file, return it as a new string.  Otherwise, return
  172.    a NULL pointer. */
  173. static char *
  174. info_file_in_path (filename, path)
  175.      char *filename, *path;
  176. {
  177.   struct stat finfo;
  178.   char *temp_dirname;
  179.   int statable, dirname_index;
  180.  
  181.   dirname_index = 0;
  182.  
  183.   while (temp_dirname = extract_colon_unit (path, &dirname_index))
  184.     {
  185.       register int i, pre_suffix_length;
  186.       char *temp;
  187.  
  188.       /* Expand a leading tilde if one is present. */
  189.       if (*temp_dirname == '~')
  190.     {
  191.       char *expanded_dirname;
  192.  
  193.       expanded_dirname = tilde_expand_word (temp_dirname);
  194.       free (temp_dirname);
  195.       temp_dirname = expanded_dirname;
  196.     }
  197.  
  198.       temp = (char *)xmalloc (30 + strlen (temp_dirname) + strlen (filename));
  199.       strcpy (temp, temp_dirname);
  200.       if (temp[(strlen (temp)) - 1] != '/')
  201.     strcat (temp, "/");
  202.       strcat (temp, filename);
  203.  
  204.       pre_suffix_length = strlen (temp);
  205.  
  206.       free (temp_dirname);
  207.  
  208.       for (i = 0; info_suffixes[i]; i++)
  209.     {
  210.       strcpy (temp + pre_suffix_length, info_suffixes[i]);
  211.  
  212.       statable = (stat (temp, &finfo) == 0);
  213.  
  214.       /* If we have found a regular file, then use that.  Else, if we
  215.          have found a directory, look in that directory for this file. */
  216.       if (statable)
  217.         {
  218.           if (S_ISREG (finfo.st_mode))
  219.         {
  220.           return (temp);
  221.         }
  222.           else if (S_ISDIR (finfo.st_mode))
  223.         {
  224.           char *newpath, *filename_only, *newtemp;
  225.  
  226.           newpath = savestring (temp);
  227.           filename_only = filename_non_directory (filename);
  228.           newtemp = info_file_in_path (filename_only, newpath);
  229.  
  230.           free (newpath);
  231.           if (newtemp)
  232.             {
  233.               free (temp);
  234.               return (newtemp);
  235.             }
  236.         }
  237.         }
  238.       else
  239.         {
  240.           /* Add various compression suffixes to the name to see if
  241.          the file is present in compressed format. */
  242.           register int j, pre_compress_suffix_length;
  243.  
  244.           pre_compress_suffix_length = strlen (temp);
  245.  
  246.           for (j = 0; compress_suffixes[j].suffix; j++)
  247.         {
  248.           strcpy (temp + pre_compress_suffix_length,
  249.               compress_suffixes[j].suffix);
  250.  
  251.           statable = (stat (temp, &finfo) == 0);
  252.           if (statable && (S_ISREG (finfo.st_mode)))
  253.             return (temp);
  254.         }
  255.         }
  256.     }
  257.       free (temp);
  258.     }
  259.   return ((char *)NULL);
  260. }
  261.  
  262. /* Given a string containing units of information separated by colons,
  263.    return the next one pointed to by IDX, or NULL if there are no more.
  264.    Advance IDX to the character after the colon. */
  265. char *
  266. extract_colon_unit (string, idx)
  267.      char *string;
  268.      int *idx;
  269. {
  270.   register int i, start;
  271.  
  272.   i = start = *idx;
  273.   if ((i >= strlen (string)) || !string)
  274.     return ((char *) NULL);
  275.  
  276.   while (string[i] && string[i] != ':')
  277.     i++;
  278.   if (i == start)
  279.     {
  280.       return ((char *) NULL);
  281.     }
  282.   else
  283.     {
  284.       char *value = (char *) xmalloc (1 + (i - start));
  285.       strncpy (value, &string[start], (i - start));
  286.       value[i - start] = '\0';
  287.       if (string[i])
  288.     ++i;
  289.       *idx = i;
  290.       return (value);
  291.     }
  292. }
  293.  
  294. /* A structure which associates a filename with its expansion. */
  295. typedef struct {
  296.   char *filename;
  297.   char *expansion;
  298. } FILENAME_LIST;
  299.  
  300. /* An array of remembered arguments and results. */
  301. static FILENAME_LIST **names_and_files = (FILENAME_LIST **)NULL;
  302. static int names_and_files_index = 0;
  303. static int names_and_files_slots = 0;
  304.  
  305. /* Find the result for having already called info_find_fullpath () with
  306.    FILENAME. */
  307. static char *
  308. lookup_info_filename (filename)
  309.      char *filename;
  310. {
  311.   if (filename && names_and_files)
  312.     {
  313.       register int i;
  314.       for (i = 0; names_and_files[i]; i++)
  315.     {
  316.       if (strcmp (names_and_files[i]->filename, filename) == 0)
  317.         return (names_and_files[i]->expansion);
  318.     }
  319.     }
  320.   return (char *)NULL;;
  321. }
  322.  
  323. /* Add a filename and its expansion to our list. */
  324. static void
  325. remember_info_filename (filename, expansion)
  326.      char *filename, *expansion;
  327. {
  328.   FILENAME_LIST *new;
  329.  
  330.   if (names_and_files_index + 2 > names_and_files_slots)
  331.     {
  332.       int alloc_size;
  333.       names_and_files_slots += 10;
  334.  
  335.       alloc_size = names_and_files_slots * sizeof (FILENAME_LIST *);
  336.  
  337.       names_and_files =
  338.     (FILENAME_LIST **) xrealloc (names_and_files, alloc_size);
  339.     }
  340.  
  341.   new = (FILENAME_LIST *)xmalloc (sizeof (FILENAME_LIST));
  342.   new->filename = savestring (filename);
  343.   new->expansion = expansion ? savestring (expansion) : (char *)NULL;
  344.  
  345.   names_and_files[names_and_files_index++] = new;
  346.   names_and_files[names_and_files_index] = (FILENAME_LIST *)NULL;
  347. }
  348.  
  349. static void
  350. maybe_initialize_infopath ()
  351. {
  352.   if (!infopath_size)
  353.     {
  354.       infopath = (char *)
  355.     xmalloc (infopath_size = (1 + strlen (DEFAULT_INFOPATH)));
  356.  
  357.       strcpy (infopath, DEFAULT_INFOPATH);
  358.     }
  359. }
  360.  
  361. /* Add PATH to the list of paths found in INFOPATH.  2nd argument says
  362.    whether to put PATH at the front or end of INFOPATH. */
  363. void
  364. info_add_path (path, where)
  365.      char *path;
  366.      int where;
  367. {
  368.   int len;
  369.  
  370.   if (!infopath)
  371.     {
  372.       infopath = (char *)xmalloc (infopath_size = 200 + strlen (path));
  373.       infopath[0] = '\0';
  374.     }
  375.  
  376.   len = strlen (path) + strlen (infopath);
  377.  
  378.   if (len + 2 >= infopath_size)
  379.     infopath = (char *)xrealloc (infopath, (infopath_size += (2 * len) + 2));
  380.  
  381.   if (!*infopath)
  382.     strcpy (infopath, path);
  383.   else if (where == INFOPATH_APPEND)
  384.     {
  385.       strcat (infopath, ":");
  386.       strcat (infopath, path);
  387.     }
  388.   else if (where == INFOPATH_PREPEND)
  389.     {
  390.       char *temp = savestring (infopath);
  391.       strcpy (infopath, path);
  392.       strcat (infopath, ":");
  393.       strcat (infopath, temp);
  394.       free (temp);
  395.     }
  396. }
  397.  
  398. /* Make INFOPATH have absolutely nothing in it. */
  399. void
  400. zap_infopath ()
  401. {
  402.   if (infopath)
  403.     free (infopath);
  404.  
  405.   infopath = (char *)NULL;
  406.   infopath_size = 0;
  407. }
  408.  
  409. /* Read the contents of PATHNAME, returning a buffer with the contents of
  410.    that file in it, and returning the size of that buffer in FILESIZE.
  411.    FINFO is a stat struct which has already been filled in by the caller.
  412.    If the file cannot be read, return a NULL pointer. */
  413. char *
  414. filesys_read_info_file (pathname, filesize, finfo)
  415.      char *pathname;
  416.      long *filesize;
  417.      struct stat *finfo;
  418. {
  419.   *filesize = filesys_error_number = 0;
  420.  
  421.   if (compressed_filename_p (pathname))
  422.     return (filesys_read_compressed (pathname, filesize, finfo));
  423.   else
  424.     {
  425.       int descriptor;
  426.       char *contents;
  427.  
  428.       descriptor = open (pathname, O_RDONLY, 0666);
  429.  
  430.       /* If the file couldn't be opened, give up. */
  431.       if (descriptor < 0)
  432.     {
  433.       filesys_error_number = errno;
  434.       return ((char *)NULL);
  435.     }
  436.  
  437.       /* Try to read the contents of this file. */
  438.       contents = (char *)xmalloc (1 + finfo->st_size);
  439.       if ((read (descriptor, contents, finfo->st_size)) != finfo->st_size)
  440.     {
  441.       filesys_error_number = errno;
  442.       close (descriptor);
  443.       free (contents);
  444.       return ((char *)NULL);
  445.     }
  446.  
  447.       close (descriptor);
  448.  
  449.       *filesize = finfo->st_size;
  450.       return (contents);
  451.     }
  452. }
  453.  
  454. /* Typically, pipe buffers are 4k. */
  455. #define BASIC_PIPE_BUFFER (4 * 1024)
  456.  
  457. /* We use some large multiple of that. */
  458. #define FILESYS_PIPE_BUFFER_SIZE (16 * BASIC_PIPE_BUFFER)
  459.  
  460. char *
  461. filesys_read_compressed (pathname, filesize, finfo)
  462.      char *pathname;
  463.      long *filesize;
  464.      struct stat *finfo;
  465. {
  466.   FILE *stream;
  467.   char *command, *decompressor;
  468.   char *contents = (char *)NULL;
  469.  
  470.   *filesize = filesys_error_number = 0;
  471.  
  472.   decompressor = filesys_decompressor_for_file (pathname);
  473.  
  474.   if (!decompressor)
  475.     return ((char *)NULL);
  476.  
  477.   command = (char *)xmalloc (10 + strlen (pathname) + strlen (decompressor));
  478.   sprintf (command, "%s < %s", decompressor, pathname);
  479.  
  480. #if !defined (BUILDING_LIBRARY)
  481.   if (info_windows_initialized_p)
  482.     {
  483.       char *temp;
  484.  
  485.       temp = (char *)xmalloc (5 + strlen (command));
  486.       sprintf (temp, "%s...", command);
  487.       message_in_echo_area ("%s", temp);
  488.       free (temp);
  489.     }
  490. #endif /* !BUILDING_LIBRARY */
  491.  
  492.   stream = popen (command, "r");
  493.   free (command);
  494.  
  495.   /* Read chunks from this file until there are none left to read. */
  496.   if (stream)
  497.     {
  498.       int offset, size;
  499.       char *chunk;
  500.     
  501.       offset = size = 0;
  502.       chunk = (char *)xmalloc (FILESYS_PIPE_BUFFER_SIZE);
  503.  
  504.       while (1)
  505.     {
  506.       int bytes_read;
  507.  
  508.       bytes_read = fread (chunk, 1, FILESYS_PIPE_BUFFER_SIZE, stream);
  509.  
  510.       if (bytes_read + offset >= size)
  511.         contents = (char *)xrealloc
  512.           (contents, size += (2 * FILESYS_PIPE_BUFFER_SIZE));
  513.  
  514.       memcpy (contents + offset, chunk, bytes_read);
  515.       offset += bytes_read;
  516.       if (bytes_read != FILESYS_PIPE_BUFFER_SIZE)
  517.         break;
  518.     }
  519.  
  520.       free (chunk);
  521.       pclose (stream);
  522.       contents = (char *)xrealloc (contents, offset + 1);
  523.       *filesize = offset;
  524.     }
  525.   else
  526.     {
  527.       filesys_error_number = errno;
  528.     }
  529.  
  530. #if !defined (BUILDING_LIBARARY)
  531.   if (info_windows_initialized_p)
  532.     unmessage_in_echo_area ();
  533. #endif /* !BUILDING_LIBRARY */
  534.   return (contents);
  535. }
  536.  
  537. /* Return non-zero if FILENAME belongs to a compressed file. */
  538. int
  539. compressed_filename_p (filename)
  540.      char *filename;
  541. {
  542.   char *decompressor;
  543.  
  544.   /* Find the final extension of this filename, and see if it matches one
  545.      of our known ones. */
  546.   decompressor = filesys_decompressor_for_file (filename);
  547.  
  548.   if (decompressor)
  549.     return (1);
  550.   else
  551.     return (0);
  552. }
  553.  
  554. /* Return the command string that would be used to decompress FILENAME. */
  555. char *
  556. filesys_decompressor_for_file (filename)
  557.      char *filename;
  558. {
  559.   register int i;
  560.   char *extension = (char *)NULL;
  561.  
  562.   /* Find the final extension of FILENAME, and see if it appears in our
  563.      list of known compression extensions. */
  564.   for (i = strlen (filename) - 1; i > 0; i--)
  565.     if (filename[i] == '.')
  566.       {
  567.     extension = filename + i;
  568.     break;
  569.       }
  570.  
  571.   if (!extension)
  572.     return ((char *)NULL);
  573.  
  574.   for (i = 0; compress_suffixes[i].suffix; i++)
  575.     if (strcmp (extension, compress_suffixes[i].suffix) == 0)
  576.       return (compress_suffixes[i].decompressor);
  577.  
  578.   return ((char *)NULL);
  579. }
  580.  
  581. /* The number of the most recent file system error. */
  582. int filesys_error_number = 0;
  583.  
  584. #if !defined (HAVE_STRERROR)
  585. extern char *sys_errlist[];
  586. extern int sys_nerr;
  587.  
  588. char *
  589. strerror (num)
  590.      int num;
  591. {
  592.   if (num >= sys_nerr)
  593.     return ("");
  594.   else
  595.     return (sys_errlist[num]);
  596. }
  597. #endif /* !HAVE_STRERROR */
  598.  
  599. /* A function which returns a pointer to a static buffer containing
  600.    an error message for FILENAME and ERROR_NUM. */
  601. static char *errmsg_buf = (char *)NULL;
  602. static int errmsg_buf_size = 0;
  603.  
  604. char *
  605. filesys_error_string (filename, error_num)
  606.      char *filename;
  607.      int error_num;
  608. {
  609.   int len;
  610.   char *result;
  611.  
  612.   if (error_num == 0)
  613.     return ((char *)NULL);
  614.  
  615.   result = strerror (error_num);
  616.  
  617.   len = 4 + strlen (filename) + strlen (result);
  618.   if (len >= errmsg_buf_size)
  619.     errmsg_buf = (char *)xrealloc (errmsg_buf, (errmsg_buf_size = 2 + len));
  620.  
  621.   sprintf (errmsg_buf, "%s: %s", filename, result);
  622.   return (errmsg_buf);
  623. }
  624.  
  625.